<template>
	<uni-loading v-if="loading"></uni-loading>
	<view class="body" v-else>
		<!--  顶部  -->
		<top-tab :options="stockList" :locationName="locationName" @stockChange="stockChange"
			@handleBarnChoose="handleBarnChoose"></top-tab>
		<!--  列表  -->
		<template v-if="purchaseList && purchaseList.length > 0">
			<uni-swipe-action>
				<uni-swipe-action-item :left-options="options" @click="handleDelete(item)" v-for="item in purchaseList">
					<view class="panel" @click="openDetail(item)">
						<view class="panel_title">
							<view class="panel_title_left">{{ item.materialName }}</view>
						</view>
						<view class="panel_content">
							<view class="panel_content">
								<view class="panel_content_row">
									<view class="panel_content_row_item">
										<view class="panel_content_row_item_title">编码：</view>
										<view class="panel_content_row_item_value">{{ item.materialNo }}</view>
									</view>
								</view>
							</view>
							<view class="panel_content">
								<view class="panel_content_row">
									<view class="panel_content_row_item">
										<view class="panel_content_row_item_title">批次：</view>
										<view class="panel_content_row_item_value">{{ item.batchNo }}</view>
									</view>
									<view class="panel_content_row_item">
										<view class="panel_content_row_item_title">规格：</view>
										<view class="panel_content_row_item_value">{{ item.materialSpec }}</view>
									</view>
								</view>
							</view>
							<view class="panel_dashboard">
								<view class="panel_dashboard_item">
									<view class="panel_dashboard_item_number success">{{ item.boxCount }}</view>
									<view class="panel_dashboard_item_value">箱数</view>
								</view>
								<view class="panel_dashboard_item">
									<view class="panel_dashboard_item_number error">{{ item.count }}{{ item.unit }}
									</view>
									<view class="panel_dashboard_item_value">总重</view>
								</view>
							</view>
						</view>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</template>
		<uni-empty-info v-else label="暂无物料信息"></uni-empty-info>

		<!--  底部按钮  -->
		<view class="footerPanel">
			<view class="footerPanel_body">
				<button @click="handleSuccess" class="footerBtn scan" type="primary"><i
						class="iconfont icon-duihao btn_icon"></i>完成
				</button>
			</view>
		</view>

		<BarnPopup ref="barnPopup" @confirm="onLocationChange"></BarnPopup>
	</view>
</template>
<script>
	import topTab from "@/components/top-tab/top-tab"
	import uniLoading from "../../vBase/components/uni-loading/uni-loading"
	import uniEmptyInfo from "../../vBase/components/uni-empty-info/uni-empty-info"
	import BarnPopup from "@/components/popup/barnPopup"

	export default {
		components: {
			topTab,
			uniLoading,
			uniEmptyInfo,
			BarnPopup
		},
		data() {
			return {
				loading: false,
				purchaseList: [],
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#FF4E74'
					}
				}],
				scaningShow: false,
				scanNo: null,
				isFouce: false,
				stockList: [],
				stockNo: '',
				locationNo: '',
				locationName: '',
				sourceStockNo: null,
				sourceLocationNo: null
			}
		},
		/**
		 * 返回指定页面(onBackPress函数放置)
		 *
		 * @param e
		 */
		// onBackPress(e) {
		//   uni.navigateBack({
		// 	  delta: 2
		//   })
		//   return true;
		// },
		async onLoad(e) {
			await this.getStockList();
			if (e?.stockNo) {
				this.sourceStockNo = e.stockNo
				this.sourceLocationNo = e.locationNo
				this.initData()
			}
		},
		onUnload() {
			uni.$off('deleteItem');
		},
		mounted() {
			let self = this
			uni.$on('deleteItem', ({
				material,
				success
			}) => {
				uni.showModal({
					title: '提示',
					content: '确认删除吗？',
					success(res) {
						if (res.confirm) {
							self.$api.deleteAllocationMaterialBox({
								id: material.id
							}, {
								loading: true
							}).then((res) => {
								if (!res.res) return;
								self.initData();
								success && success();
							});
						}
					}
				})
			})
		},
		methods: {
			/** 查询仓库*/
			async getStockList() {
				this.loading = true;
				try {
					let {
						res,
						stockList = []
					} = await this.$api.getCustomerStock();
					if (!res) return;
					this.stockList = stockList;
					if (stockList.length) this.stockNo = stockList[0].stockNo;
				} finally {
					this.loading = false;
				}
			},
			/** 初始化数据 */
			initData() {
				if (!this.stockNo) return;
				this.pageConfig = {
					pageIndex: 0,
					pageSize: 20,
					bottomEnd: false,
					bottomLoading: true
				}
				this.getDataList()
			},

			/** 获取数据 */
			async getDataList() {
				let {
					res,
					data = []
				} = await this.$api.getAllocationUserMaterial({
					stockNo: this.sourceStockNo
				});
				if (!res) return;
				this.purchaseList = data;
				this.loading = false;
				this.pageConfig.bottomEnd = true;
				this.pageConfig.bottomLoading = false;
				uni.stopPullDownRefresh();
			},

			/** 完成新增 */
			async handleSuccess() {
				try {
					let trayNumber = uni.getStorageSync("transfer") ?? ""
					if (!this.stockNo) {
						uni.showToast({
							title: '请选择仓库',
							icon: 'none'
						});
						return;
					}
					if (!this.locationNo) {
						uni.showToast({
							title: '请选择仓位',
							icon: 'none'
						});
						return;
					}
					if (!this.purchaseList || !this.purchaseList.length) {
						uni.showToast({
							title: '请添加物料信息',
							icon: 'none'
						});
						return;
					}
					let res = await this.$api.saveUserAllocation({
						toStockNo: this.stockNo,
						toLocationNo: this.locationNo,
						stockNo: this.sourceStockNo,
						locationNo: this.sourceLocationNo,
						allocationType: 'pda',
					}, {
						loading: true
					})
					if (!res.res) return
					if (res.billNo) {
						let param = Object.assign({
							billNo: res.billNo,
							beforeStockNo: this.sourceStockNo,
							toStockNo: this.stockNo
						}, trayNumber ? {
							trayNumber: trayNumber
						} : {})
						let billRes = await this.$api.auditUserAllocation(param)
						if (!billRes.res) return
					}
					uni.showToast({
						title: "调拨完成",
						icon: "none"
					})
					setTimeout(() => {
						uni.navigateBack({
							delta: 2
						})
					}, 500)
				} finally {}

			},

			/**
			 * 获取详情
			 */
			async openDetail(material) {
				let {
					res,
					data = []
				} = await this.$api.getAllocationUserMaterialBox({
					detailId: material.id,
				}, {
					loading: true
				});
				let dataList = data.map(item => {
					return {
						id: item.id,
						materialName: material.materialName,
						materialNo: material.materialNo,
						stockNo: material.stockNo,
						batchNo: material.batchNo,
						boxNo: item.boxNo,
						count: item.count,
						unit: item.unit,
					}
				})
				this.openPopNVue('入库明细', dataList)
			},

			/** 选择仓位 */
			async onLocationChange(location) {
				this.locationNo = location.locationNo;
				this.locationName = location.locationName;
				let res = await this.$api.updateAllAllocation({
					toStockNo: this.stockNo,
					toLocationNo: this.locationNo,
					allocationType: "pda",
				})
				if (!res.res) return
			},

			/**
			 * 选择仓库
			 * @param e
			 */
			stockChange(e) {
				this.stockNo = this.stockList[e].stockNo
				// this.initData()
			},

			/**
			 * 选择仓位
			 * @param e
			 */
			stockLocation(e) {
				this.locationNo = e.locationNo
				this.locationName = e.locationName
			},

			/**
			 * 选择
			 */
			handleBarnChoose() {
				this.$refs.barnPopup.open(this.stockNo);
			},

			/** 扫码 */
			async handleScanCode() {
				this.$refs.scaningPopup.open(this.stockNo, this.locationNo)
			},

			/**
			 * 删除物料
			 * @param {Object} e
			 */
			handleDelete(material) {
				const self = this
				uni.showModal({
					title: '提示',
					content: '是否删除物料？',
					success(res) {
						if (res.confirm) {
							self.$api.deleteAllocationMaterial({
								id: material.id
							}, {
								loading: true
							}).then(res => {
								if (res.res) {
									uni.showToast({
										title: '物料删除成功',
										icon: 'none'
									})
									uni.removeStorageSync("transfer")
									self.initData();
								}
							})
						}
					}
				})
			},

			/**
			 * 下拉刷新数据
			 */
			onPullDownRefresh() {
				this.initData()
			},

			/** 上滑加载 */
			onReachBottom() {
				if (!this.pageConfig.bottomEnd) {
					this.pageConfig.pageIndex++
					this.pageConfig.bottomLoading = true
					this.getPurchaseList();
				}
			},
		}
	}
</script>
<style lang="scss" scoped>
	@import "../../static/css/common";
</style>
